import java.net.*;
import java.io.*;
import java.awt.*;
import java.util.*;

public class Cliente extends Frame implements Runnable{
     // public Juego subject;
      protected DataInputStream i;
	  protected DataOutputStream o;
	  protected TextArea output;
	  protected TextField input;
	  protected Thread listener;
	  public boolean hit=true;
	  public int[] cartas = new int [5];
	  int y=0; 
	  public int sumaPuntos;
	  public int puntosServer;
	  
	  
    public void Update() {
    
    }
    
    public void Pantalla() {
    
    }
    
    public void Hitme() {
    	System.out.print("antes del Hitme" + "\n");
    	
		try {
		if(true){	
		o.writeUTF("hitme");
		
		GuardarCartas();
		System.out.print("Pedi carta extra" + "\n");
		}else{
		o.writeUTF("stand");
		System.out.print("No pedi carta extra" + "\n");	
		}
	 
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
    	
    }
    
    public void VoltearCarta() {
    
    }
    
    
    public boolean CompararRonda() throws IOException{
    	
    	sumatoria();
    	
    	
    	if(sumaPuntos>21){
    		o.writeUTF("standPerdi");
    		return false;
    	}
    	if((sumaPuntos==21)&&(cartas[2]==0)){
    		o.writeUTF("stand");
    	    return false;
    	}
    	if((cartas[4]!=0)&&(sumaPuntos<=21)){
    		o.writeUTF("stand");
    		return false;
    	}
    	
    	return true;
    }
    
    public void CompararFinal() throws IOException {
    
    	sumatoria();
    				
    	if((sumaPuntos==21)&&(cartas[2]==0))
    		o.writeUTF("gane");
    	
    	if((cartas[4]!=0)&&(sumaPuntos<=21))
    		o.writeUTF("gane");
    	
    	if(sumaPuntos>puntosServer)
    		o.writeUTF("gane");

    	if(sumaPuntos<puntosServer)
    		o.writeUTF("perdi");
    	
    }
    
    public void sumatoria(){
    	
    	int sum=0;
    	
    	for(int i=0;i<5;i++){
    		if((cartas[i]==111)&& (sumaPuntos<11))
    			cartas[i]=11;
    		if((cartas[i]==111)&& (sumaPuntos>11)) 
    			cartas[i]=1;
    			
    		sum=sum+cartas[i];	
    	}
    	sumaPuntos=sum;
    	
    }
    
    public Cliente (String title, InputStream i, OutputStream o) {
	    super (title);
	    this.i = new DataInputStream (new BufferedInputStream (i));
	    this.o = new DataOutputStream (new BufferedOutputStream (o));
	    setLayout (new BorderLayout ());
	    add ("Center", output = new TextArea ());
	    output.setEditable (false);
	    add ("South", input = new TextField ());
	    pack ();
	    show ();
	    input.requestFocus ();
	    listener = new Thread (this);
	    listener.start ();
	  }

	
	public void update(Observable o, Object arg) {
		// TODO Auto-generated method stub
		
	}

	public void run() {
		try {
		      while (true) {
		    	
		        String line = i.readUTF ();
		        output.append(line + "\n");
		        System.out.print(line+"\n");
   
		        if(line.equals("carta")){	
		        	GuardarCartas();
		        	sumatoria();
		        	System.out.print("Sumatota:"+sumaPuntos+"\n");
		        }

		        if(line.equals("pregunta")){
		        	System.out.print("hizo una pregunta"+"\n");
		            if(true){
		        	Hitme();
		        	sumatoria();
		        	output.append("SumatotaHit:"+sumaPuntos+"\n");
		            }
		        }
		      }
		    } catch (IOException ex) {
		      ex.printStackTrace ();
		    } finally {
		      listener = null;
		      input.hide ();
		      validate ();
		      try {
		        o.close ();
		      } catch (IOException ex) {
		        ex.printStackTrace ();
		      }
		    }
		
	}
	
	private void GuardarCartas() {
		// TODO Auto-generated method stub
		TraducirCartas tc=new TraducirCartas();
		
		String l;
		try {
			l = i.readUTF();
	    	output.append(l + "\n");
	    	int car=Integer.parseInt(l);
	    	int realcar=tc.Transformar(car);
	    System.out.print(" Guardando "+ car+"\n");
	    cartas[y]=realcar;	    
	    System.out.print(" Carta "+ cartas[y]+"\n");
	    y++;
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}
	

	public boolean handleEvent (Event e) {
	    if ((e.target == input) && (e.id == Event.ACTION_EVENT)) {
	      try {
	        o.writeUTF ((String) e.arg);
	            o.flush (); 
	      } catch (IOException ex) {
	        ex.printStackTrace();
	        listener.stop ();
	      }
	      input.setText ("");
	      return true;
	    } else if ((e.target == this) && (e.id == Event.WINDOW_DESTROY)) {
	      if (listener != null)
	        listener.stop ();
	      hide ();
	      return true;
	    }
	    return super.handleEvent (e);
	  }
	
	public static void main (String args[]) throws IOException {
	    
		  //if (args.length != 2)
	      //throw new RuntimeException ("Syntax: ChatClient ");
		
	    Socket s = new Socket ("localhost", 6000);
	    new Cliente ("Black Jack",
	                    s.getInputStream (), s.getOutputStream ());
	    
	  }
	
}
